package com.ruoyi.project.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.utils.pay.AlipayUtils;
import com.ruoyi.project.common.domain.Trade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysAliPayMapper;
import com.ruoyi.project.system.domain.SysAliPay;
import com.ruoyi.project.system.service.ISysAliPayService;

/**
 * aliPayService业务层处理
 *
 * @author hongwj
 * @date 2023-08-15
 */
@Service
@Slf4j
public class SysAliPayServiceImpl implements ISysAliPayService {
    @Autowired
    private SysAliPayMapper sysAliPayMapper;

    @Autowired
    private AlipayUtils alipayUtils;

    /**
     * 查询aliPay
     *
     * @param configId aliPay主键
     * @return aliPay
     */
    @Override
    public SysAliPay selectSysAliPayByConfigId(Long configId) {
        return sysAliPayMapper.selectSysAliPayByConfigId(configId);
    }

    /**
     * 查询aliPay列表
     *
     * @param sysAliPay aliPay
     * @return aliPay
     */
    @Override
    public List<SysAliPay> selectSysAliPayList(SysAliPay sysAliPay) {
        return sysAliPayMapper.selectSysAliPayList(sysAliPay);
    }

    /**
     * 新增aliPay
     *
     * @param sysAliPay aliPay
     * @return 结果
     */
    @Override
    public int insertSysAliPay(SysAliPay sysAliPay) {
        return sysAliPayMapper.insertSysAliPay(sysAliPay);
    }

    /**
     * 修改aliPay
     *
     * @param sysAliPay aliPay
     * @return 结果
     */
    @Override
    public int updateSysAliPay(SysAliPay sysAliPay) {
        return sysAliPayMapper.updateSysAliPay(sysAliPay);
    }

    /**
     * 批量删除aliPay
     *
     * @param configIds 需要删除的aliPay主键
     * @return 结果
     */
    @Override
    public int deleteSysAliPayByConfigIds(Long[] configIds) {
        return sysAliPayMapper.deleteSysAliPayByConfigIds(configIds);
    }

    /**
     * 删除aliPay信息
     *
     * @param configId aliPay主键
     * @return 结果
     */
    @Override
    public int deleteSysAliPayByConfigId(Long configId) {
        return sysAliPayMapper.deleteSysAliPayByConfigId(configId);
    }


    public String toPayAsPc(Trade trade) throws Exception {
        SysAliPay alipay = sysAliPayMapper.selectMainConfig();
        if (AlipayUtils.SECRET_MODE.equals(alipay.getMode())) {
            AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
            // 创建API对应的request(电脑网页版)
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 订单完成后返回的页面和异步通知地址
            request.setReturnUrl(alipay.getReturnUrl());
            request.setNotifyUrl(alipay.getNotifyUrl());
            Map<String, Object> param = new HashMap<>();
            param.put("out_trade_no", trade.getOutTradeNo());
            param.put("product_code", "FAST_INSTANT_TRADE_PAY");
            param.put("total_amount", trade.getTotalAmount());
            param.put("subject", trade.getSubject());
            param.put("body", trade.getBody());
            param.put("extend_params", ImmutableMap.of("sys_service_provider_id", alipay.getSysServiceProviderId()));
            request.setBizContent(JSON.toJSONString(param));//填充业务参数
            return alipayClient.pageExecute(request, "GET").getBody();
        } else {
            String privateKey = alipay.getPrivateKey();
            CertAlipayRequest alipayConfig = new CertAlipayRequest();
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setServerUrl(alipay.getGatewayUrl());
            alipayConfig.setAppId(alipay.getAppId());
            alipayConfig.setCharset("UTF8");
            alipayConfig.setSignType("RSA2");
            alipayConfig.setFormat("json");
            alipayConfig.setCertPath(alipay.getCertPath());
            alipayConfig.setAlipayPublicCertPath(alipay.getPublicCertpath());
            alipayConfig.setRootCertPath(alipay.getRootCertPath());
            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setReturnUrl(alipay.getReturnUrl());
            request.setNotifyUrl(alipay.getNotifyUrl());
            Map<String, Object> param = new HashMap<>();
            param.put("out_trade_no", trade.getOutTradeNo());
            param.put("product_code", "FAST_INSTANT_TRADE_PAY");
            param.put("total_amount", trade.getTotalAmount());
            param.put("subject", trade.getSubject());
            param.put("body", trade.getBody());
            request.setBizContent(JSON.toJSONString(param));//填充业务参数
            return alipayClient.pageExecute(request, "GET").getBody();
        }
    }

    @Override
    public String toPayAsWeb(Trade trade) throws Exception {
        SysAliPay alipay = sysAliPayMapper.selectMainConfig();
        if (AlipayUtils.SECRET_MODE.equals(alipay.getMode())) {
            AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
            request.setReturnUrl(alipay.getReturnUrl());
            request.setNotifyUrl(alipay.getNotifyUrl());
            Map<String, Object> param = new HashMap<>();
            param.put("out_trade_no", trade.getOutTradeNo());
            param.put("product_code", "QUICK_WAP_WAY");
            param.put("total_amount", trade.getTotalAmount());
            param.put("subject", trade.getSubject());
            param.put("body", trade.getBody());
            param.put("extend_params", ImmutableMap.of("sys_service_provider_id", alipay.getSysServiceProviderId()));
            request.setBizContent(JSON.toJSONString(param));//填充业务参数
            return alipayClient.pageExecute(request, "GET").getBody();
        } else {
            String privateKey = alipay.getPrivateKey();
            CertAlipayRequest alipayConfig = new CertAlipayRequest();
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setServerUrl(alipay.getGatewayUrl());
            alipayConfig.setAppId(alipay.getAppId());
            alipayConfig.setCharset("UTF8");
            alipayConfig.setSignType("RSA2");
            alipayConfig.setFormat("json");
            alipayConfig.setCertPath(alipay.getCertPath());
            alipayConfig.setAlipayPublicCertPath(alipay.getPublicCertpath());
            alipayConfig.setRootCertPath(alipay.getRootCertPath());
            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
            request.setReturnUrl(alipay.getReturnUrl());
            request.setNotifyUrl(alipay.getNotifyUrl());
            Map<String, Object> param = new HashMap<>();
            param.put("out_trade_no", trade.getOutTradeNo());
            param.put("product_code", "QUICK_WAP_WAY");
            param.put("total_amount", trade.getTotalAmount());
            param.put("subject", trade.getSubject());
            param.put("body", trade.getBody());
            request.setBizContent(JSON.toJSONString(param));//填充业务参数
            return alipayClient.pageExecute(request, "GET").getBody();
        }
    }
}
